const Koa = require('koa');
const router = require('koa-router')();
const koaBody = require('koa-body');
const koaCors = require('koa-cors');

//国际化
const convert = require('koa-convert');
const locale = require('koa-locale');
const i18n = require('koa-i18n');
// 国际化引入结束
const catchError = require('./middlewares/catchErrorMidllerware');
const routerResponseMiddle = require('./middlewares/routerReponseMiddleware');

const env= require('./utils/env')


const ENV = process.env.NODE_ENV;

//配置文件引入
const appConfig = require('./configs/appConfig');



const app = new Koa();

locale(app);
app.use(i18n(app, {
    directory: appConfig.I18N_OPTIONS.i18nDir,
    locales: appConfig.I18N_OPTIONS.locales,
    modes: appConfig.I18N_OPTIONS.modes
}));

// 请求体解析中间件
app.use(koaBody({
    multipart: true
}));

// 跨域处理
app.use(koaCors(appConfig.CROS_OPTIONS));

// 统一返回格式
app.use(routerResponseMiddle(appConfig.RESPONSE_OPTIONS));
//全局异常处理
app.use(catchError);


/************************************
 *
 * 路由绑定
 *
 ************************************/
// 引入路由
const registerRouter = require("./routers")
// 引入路由结束
router.get("/", async (ctx, next) => {
    ctx.body = "这里是首页呀";
});


app.use(router.routes()).use(router.allowedMethods())
app.use(registerRouter())


/************************************
 *
 * 路由绑定结束
 *
 ************************************/

app.listen(appConfig.SERVER_PORT);
console.log('server start in port ' + appConfig.SERVER_PORT);
